In [12]:
    
from __future__ import division
from sympy import *
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
init_printing(use_latex='mathjax') # this makes the output look like real math symbols
    
In [14]:
    
diff(x**4 + cos(x**2), x)
    
    Out[14]:
In [15]:
    
integrate(x**4+ cos(x**2), x)
    
    Out[15]:
In [17]:
    
test = integrate(x**4+ cos(x**2)*sin(x**2), x)
    
In [18]:
    
test
    
    Out[18]:
In [19]:
    
sqrt(8)
    
    Out[19]:
In [20]:
    
x+y**2
    
    Out[20]:
In [21]:
    
I**2
    
    Out[21]:
In [22]:
    
Rational(1, 55)
    
    Out[22]:
In [25]:
    
rat = Rational(1, 55) + Rational(58, 327)
rat
    
    Out[25]:
In [26]:
    
N(rat)
    
    Out[26]:
In [28]:
    
N(pi, 1000) # 1000 is how many decimals I want
    
    Out[28]:
In [30]:
    
expr = x**2 + 2*x + 1
expr
    
    Out[30]:
In [31]:
    
expr.subs(x, 10)
    
    Out[31]:
In [33]:
    
expr = pi * x**2
expr
    
    Out[33]:
In [40]:
    
expr.subs(x, 10)
    
    Out[40]:
In [41]:
    
N(_)
    
    Out[41]:
In [51]:
    
expr = (x+y + 5)**10
expr
    
    Out[51]:
In [52]:
    
expand(expr)
    
    Out[52]:
In [53]:
    
factor(_)
    
    Out[53]:
In [59]:
    
new_expr = integrate(_, x)
new_expr
    
    Out[59]:
In [62]:
    
temp = new_expr.subs(x, 1)
temp.subs(y, 2)
    
    Out[62]:
In [65]:
    
    
    Out[65]:
In [66]:
    
sin(x)/cos(x)
    
    Out[66]:
In [67]:
    
simplify(_)
    
    Out[67]:
In [70]:
    
expr = 1/(x**2 + 2*x)
expr
    
    Out[70]:
In [71]:
    
apart(expr)
    
    Out[71]:
In [72]:
    
together(_)
    
    Out[72]:
In [73]:
    
diff(sin(x), x)
    
    Out[73]:
In [74]:
    
Integral(sin(x), (x, 0, pi))
    
    Out[74]:
In [75]:
    
N(_)
    
    Out[75]:
In [77]:
    
expr = Sum(1/(x**2 + 2*x), (x, 1, 10))
expr
    
    Out[77]:
In [78]:
    
N(_)
    
    Out[78]:
In [79]:
    
expr.doit()
    
    Out[79]:
In [81]:
    
expr = Product(1/(x**2 + 2*x), (x, 1, 10))
expr
    
    Out[81]:
In [82]:
    
expr.doit()
    
    Out[82]:
In [84]:
    
expr = 2*x + 10
expr
    
    Out[84]:
In [85]:
    
solve(expr)
    
    Out[85]:
In [87]:
    
expr = x**2 - 10
expr
    
    Out[87]:
In [88]:
    
solve(expr)
    
    Out[88]:
In [90]:
    
expr_1 = 2*x + y
expr_2 = 2*y - x + 21
    
In [91]:
    
solve([expr_1, expr_2], (x, y))
    
    Out[91]:
In [92]:
    
from sympy.physics import units as u
    
In [93]:
    
5. * u.microgram
    
    Out[93]:
In [94]:
    
1./5 * u.inch
    
    Out[94]:
In [96]:
    
1. * u.nano
    
    Out[96]:
In [97]:
    
u.watt
    
    Out[97]:
In [99]:
    
1. * u.watts
    
    Out[99]:
In [100]:
    
    
    Out[100]:
In [101]:
    
kmph = u.km / u.hour
mph = u.mile / u.hour
N(mph/kmph)
    
    Out[101]:
In [102]:
    
from sympy.plotting import plot, plot_parametric
    
In [105]:
    
expr = sin(2*sin(x**3))
plot(expr, (x, 0, 5))
    
    
    Out[105]:
In [106]:
    
expr = diff(x**5 + sin(x), x)
    
In [107]:
    
expr
    
    Out[107]:
In [108]:
    
plot(expr, (x, -5, 5))
    
    
    Out[108]:
In [109]:
    
def sympy_expr(x_val):
    expr = x**2 + sqrt(3)*x - Rational(1, 3)
    return expr.subs(x, x_val)
sympy_expr(3)
    
    Out[109]:
In [110]:
    
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
    
In [111]:
    
list1 = np.arange(1, 1000)
list2 = pd.Series(list1)
    
In [112]:
    
%timeit [sympy_expr(item) for item in list1]
%timeit [sympy_expr(item) for item in list2]
    
    
In [113]:
    
%timeit np.vectorize(sympy_expr)(list1)
%timeit list2.apply(sympy_expr)
    
    
In [114]:
    
expr = x**2 + sqrt(3)*x - Rational(1, 3)
lf = lambdify(x, expr)
    
In [115]:
    
%timeit lf(list1)
%timeit lf(list2)
    
    
In [116]:
    
fig = plt.figure()
axes = fig.add_subplot(111)
x_vals = np.linspace(-5., 5.)
y_vals = lf(x_vals)
axes.grid()
axes.plot(x_vals, y_vals)
plt.show()
    
    
In [ ]: